package com.engine.core.util;

import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Response implements Serializable {


    public final static  String RESPONSEDATA="data";

    private boolean success;

    private boolean hasData;

    private boolean showErr;

    private int code;
    private String msg;

    private Exception err;


    private Map<String,Object> mapValues=new HashMap<>();


    public  Response  success() {
        this.code=QueryUtil.ResponseCode.SUCCESS;
        this.success=true;
        return this;
    }

    public Response success(String msg) {
        this.code=QueryUtil.ResponseCode.SUCCESS;
        this.success=true;
        this.msg=msg;
        return this;
    }

    public Response fail(String msg) {
        this.setCode(QueryUtil.ResponseCode.ERROR);
        return this.setSuccess(false).setMsg(msg);
    }

    public Response set(String key,Object value){
        this.mapValues.put(key,value);
        return this;
    }

    public Object get(String key){
         return this.mapValues.get(key);
    }


    public static Response static_success(String msg) {
        Response response = new Response();
        response.success=true;
        response.msg=msg;
        return response;
    }

    public static Response static_success(String msg, int code) {
        Response response = new Response();
        response.success=true;
        response.msg=msg;
        response.code=code;
        return response;
    }


    public static Response static_fail(String msg,int code) {
        Response response = new Response();
        response.success=false;
        response.msg=msg;
        response.code=code;
        return response;
    }



    public static Response static_fail(int code) {
        return new Response().setSuccess(false).setCode(code);
    }

    public Response fail(String msg,int code)
    {
        return this.setSuccess(false).setMsg(msg).setCode(code);
    }

    public Response data(Object data){
        return this.set("data",data);
    }

    public Object data(){
        return this.get("data");
    }



    /**
     * 成功执行带消息和Code
     * @param msg
     * @param code
     * @return
     */
    public Response success(String msg,int code)
    {
        return this.setSuccess(true).setMsg(msg).setCode(code);
    }


    public boolean isSuccess() {
        return success;
    }

    public Response setSuccess(boolean success) {
        this.success = success;
        if(!success){
            this.setMsg("执行异常!");
        }
        return this;
    }

    public int getCode() {
        return code;
    }

    public Response setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Response setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Map<String, Object> getMapValues() {
        return mapValues;
    }

    public void setMapValues(Map<String, Object> mapValues) {
        this.mapValues.putAll(mapValues);
    }

    public boolean isHasData() {
        return hasData;
    }

    public void setHasData(boolean hasData) {
        this.hasData = hasData;
    }

    public String getJSONString() {
        HashMap map = new HashMap();
        map.put("success", this.success);
        map.put("code", this.code);
        map.put("msg", this.msg);

        if(err!=null){
            map.put("error", Utils.getErrorInfoFromException(err));
        }
        map.putAll(this.mapValues);
        return JSONObject.toJSONString(map);
    }


    @Override
    public String toString() {
        return getJSONString();
    }

    public Exception getErr() {
        return err;
    }

    public Response setErr(Exception err) {
        if(err!=null){
            this.err = err;
            this.code=500;
            this.setSuccess(false);
        }
        return this;
    }

    public boolean isShowErr() {
        return showErr;
    }

    public void setShowErr(boolean showErr) {
        this.showErr = showErr;
    }

    public void setAll(Response other){
        this.code=other.code;
        this.err=other.err;
        this.hasData=other.hasData;
        this.msg=other.msg;
        this.mapValues.clear();
        this.mapValues.putAll(other.mapValues);
        this.success=other.success;

    }
}
